var vue=new Vue({
    el:'#main',
    data:{
        name:'',
        code:'',
        password:'',
        rpassword:'',
        telephone:'',
        email:'',
        oldRoles:[],
        roles:[]

    },
    methods:{
        selectRoles:function () {
            axios({
                url:'/role/selectAll'
            }).then(response=>{
                this.oldRoles=response.data.obj;
                for (let i = 0; i < this.oldRoles.length; i++) {
                    Vue.set(this.oldRoles[i],"checked",false);
                }
            }).catch(error=>{
                layer.msg(error.message);
            })
        },
        addRole:function (role) {
            for (let i = 0; i < this.oldRoles.length; i++) {
                if (this.oldRoles[i].roleId==role.roleId){
                    this.oldRoles[i].checked=!this.oldRoles[i].checked;
                    if (this.oldRoles[i].checked){
                        this.roles.push(this.oldRoles[i]);
                    }
                    else {
                        for (let j = 0; j < this.roles.length; j++) {
                            if (this.oldRoles[i].roleId==this.roles[j].roleId){
                                this.roles.splice(j,1);
                            }
                        }
                    }
                }
            }
        },
        doInsert:function () {
            if (this.name==''&&this.code==''&&this.password==''
                &&this.rpassword==''&&this.telephone==''&&
                this.email==''&&this.roles==''){
                layer.msg("信息输入不完整");
                return;
            }
            if (this.password!=this.rpassword){
                layer.msg("两次密码输入不一致");
                return;
            }
            axios({
                url:'/adminInfo/doInsert',
                method:'put',
                data:{
                    name:this.name,
                    code:this.code,
                    password: this.password,
                    telephone:this.telephone,
                    email:this.email,
                    roles:this.roles
                }
            }).then(response=>{
                if (response.data.success){
                    parent.layer.message='success';
                    let index=parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            }).catch(error=>{
                layer.msg(error.method);
            })
        }
    },
    created:function () {
        this.selectRoles();
    }
});